
--[[
	登录流程

--]]

local skynet = require("skynet")
local json = require("cjson")
---------------------------------------------------------
-- class
---------------------------------------------------------
local Handler = class("Handler")

function Handler:ctor(name)

	self.client_net = new("client_net")
	self.timer = new("timer")
	self:register()
end


function Handler:register()
	--g.dispatch:register("games.RES_PING", handler(self, self.resPing))
	--g.dispatch:register("games.RES_REGISTER", handler(self, self.resRegister))
	--g.register("games.RES_DESK_MATCH", self.resDeskMatch)
	--g.register("games.RES_DESK_ENTER", self.resDeskEnter)


	g.register("get", handler(self, self.test))
	g.register("C2_GS_login", handler(self, self.resLogin))
	g.register("C2_TS_enter", handler(self, self.resDeskEnter))
	g.register("C2_TS_sit", handler(self, self.resDeskSitdown))
	g.register("TS_2C_sitBc", handler(self, self.resDeskSitdownBc))
	g.register("C2_TS_standup", handler(self, self.resDeskSitup))
	g.register("TS_2C_standupBc", handler(self, self.resDeskSitupBc))
	g.register("C2_TS_leave", handler(self, self.resDeskLeave))
	g.register("TS_2C_leaveBc", handler(self, self.resDeskLeaveBc))
	g.register("TS_2C_nextbetBc", handler(self, self.s2cNextBet))
	g.register("TS_2C_gamebeginBc", handler(self, self.s2cGameBegin))
	g.register("C2_TS_bet", handler(self, self.resBet))
	g.register("TS_2C_buttonBc", handler(self, self.s2cButton))
	g.register("TS_2C_betBc", handler(self, self.s2cBet))
	g.register("TS_2C_preflop", handler(self, self.s2cPreflop))
	g.register("TS_2C_flopBc", handler(self, self.s2cFlop))
	g.register("TS_2C_turnBc", handler(self, self.s2cTurn))
	g.register("TS_2C_riverBc", handler(self, self.s2cRiver))
	g.register("TS_2C_gameoverBc", handler(self, self.s2cGameover))
	g.register("TS_2C_potBc", handler(self, self.s2cPot))
	g.register("TS_2C_enterBc", handler(self, self.s2cEnter))
	g.register("TS_2C_upblindBc", handler(self, self.s2cUpblindBc))
	g.register("TS_2C_eventBc", handler(self, self.eventBc))
	g.register("GS_2C_updateuser", handler(self, self.ntUpdateUser))
	g.register("TS_2C_messageBc", handler(self, self.ntMessage))
	g.register("GS_2C_error", handler(self, self.ntError))
	g.register("C2_TS_message", handler(self, self.resMessage))
	g.register("TS_2C_AutoOperBc", handler(self, self.ntAutoOper))
	g.register("TS_2C_showcardsBc", handler(self, self.ntShowCards))

	g.register("C2_TS_enter_game", handler(self, self.resEnterGame))
	g.register("SN_2C_gamebegin", handler(self, self.s2cGameBegin))
	g.register("C2_SN_hit_ball", handler(self, self.resHitBall))
	g.register("SN_2C_next_oper", handler(self, self.resNextOper))
	g.register("SN_2C_hit_ball", handler(self, self.s2cHitBall))

end

function Handler:onStart()

	--连接gate
	local ip = "127.0.0.1"
	local port = 60022
	local res = self.client_net:connect(ip, port)
	if not res then
		return
	end


	--定时器
	--self.timer:start({sec=3, callback=handler(self, self.reqLogin)} )
	--self.timer:start({sec=3, callback=handler(self, self.reqLogin)} )
	--self.timer:start({sec=5, callback=handler(self, self.pingReq)} )

	self.timer:timeout(3, handler(self, self.reqLogin))
end

function Handler:test(...)
	print("test______", ...)

end

function Handler:reqPing()
	-- body
	local data = {
		time = os.time(),
	}
	self.client_net:send("games.REQ_PING", data)
end

function Handler:resPing(data)

	print("pingRes:", tostring(data))
end

--登录请求
function Handler:reqLogin( ... )
	local mid = g.getenv("account") or 123456
	local data = {
		mid = mid,
		token = "udid_20220319221824_385_65815",
	}
	self.client_net:send("C2_GS_login", data)
end

--登录返回
function Handler:resLogin(msg)
	print("resLogin data:", msg)
	self.uid = msg.mid
	self:reqDeskEnter(msg)

	--self:reqDeskEnter2()
	return
end

--普通场入桌
function Handler:reqDeskEnter(msg)
	local request = {
		mid = msg.mid,
		rid = 3,
		chips = 30000,
		rtype = 0,
		info = "",
	}
	self.client_net:send("C2_TS_enter", request)
end

--比赛场入桌
function Handler:reqDeskEnter1(msg)
	local request = {
		mid = msg.mid,
		rid = 1,
		--tid 2 : string #桌子id
		chips = 30000,
		rtype = 1, --比赛场
		info = "",
	}
	self.client_net:send("C2_TS_enter", request)
end

function Handler:reqDeskEnter2()
	local request = {
		mid = self.uid,
		rid = 1,
		--tid 2 : string #桌子id
		chips = 30000,
		rtype = 0, --比赛场
		info = "",
		gtype = 1,
	}
	self.client_net:send("C2_TS_enter_game", request)
end

function Handler:resDeskEnter(msg)
	g.log:info("resDeskEnter data:", msg)
	if msg.code > 0 then
		return
	end
	self.rid = msg.roominfo.rid
	self.tid = msg.roominfo.tid
	if msg.rtype and msg.rtype == 0 then
		g.timeout(2, function ()
			self:reqDeskSitdown()
		end)
	end
end

function Handler:resEnterGame(msg)
	g.log:info("resEnterGame data:", msg)
	if msg.code > 0 then
		return
	end
	self.rid = msg.rid
	self.tid = msg.tid
	if msg.rtype and msg.rtype == 0 then
		g.timeout(2, function ()
			self:reqDeskSitdown()
		end)
	end
end

function Handler:reqDeskLeave()
	local data = {
		mid = self.uid,
		tid = self.tid,
	}
	self.client_net:send("C2_TS_leave", data)
end
function Handler:resDeskLeave(msg)
	g.log:info("resDeskLeave data:", msg)
end
function Handler:resDeskLeaveBc(msg)
	g.log:info("resDeskLeaveBc data:", msg)
end

function Handler:reqDeskSitdown()
	local data = {
		mid = self.uid,
		tid = self.tid,
		seatid = 1,
		chips = 30000,
	}
	self.client_net:send("C2_TS_sit", data)
end
function Handler:resDeskSitdown(msg)
	g.log:info("resDeskSitdown data:", msg)

	--g.loop(550, function()
	--	--self:reqDeskSitup()
	--	self:reqMessage()
	--	if self.err_msg then
	--		return true
	--	end
	--end)
end
function Handler:resDeskSitdownBc(msg)
	g.log:info("resDeskSitdownBc data:", msg)
end

function Handler:reqDeskSitup()
	local data = {
		mid = self.uid,
		tid = self.tid,
	}
	self.client_net:send("C2_TS_standup", data)
end
function Handler:resDeskSitup(msg)
	g.log:info("resDeskSitup data:", msg)
	if self.uid ~= msg.mid then
		return
	end
	g.timeout(2, function()
		self:reqDeskLeave()
	end)
end
function Handler:resDeskSitupBc(msg)
	g.log:info("resDeskSitupBc data:", msg)
end

function Handler:s2cNextBet(msg)
	g.log:info("s2cNextBet data:", msg)
	if self.uid == msg.mid then
		local bet = msg.follow_bet
		--if math.random(1, 3) == 1 then
		--	bet = bet
		--end
		if bet > msg.max_bet then
			bet = msg.max_bet --allin
		end

		print("##############bet  ################", self.uid, bet)
		self:reqBet(msg.mid, bet)
	end
end

function Handler:s2cGameBegin(msg)
	g.log:info("s2cGameBegin data:", msg)
	self:reqHitBall()

end

function Handler:reqHitBall()
	local data = {
		mid = self.uid,
		tid = self.tid,
		dir = "-0.008353256",
		power = "1",
	}
	self.client_net:send("C2_SN_hit_ball", data)
end
function Handler:resHitBall(msg)
	g.log:info("resHitBall data:", msg)

end

function Handler:reqBet(uid, bet)
	uid = uid or self.uid
	bet = bet or 0
	local data = {
		mid = uid,
		tid = self.tid,
		chips = bet,
	}
	self.client_net:send("C2_TS_bet", data)
end
function Handler:resBet(msg)
	g.log:info("resBet data:", msg)
end

function Handler:s2cButton(msg)
	g.log:info("s2cButton data:", msg)
end

function Handler:s2cBet(msg)
	g.log:info("s2cBet data:", msg)
end

function Handler:s2cPreflop(msg)
	g.log:info("s2cPreflop data:", msg)
end

function Handler:s2cFlop(msg)
	g.log:info("s2cFlop data:", msg)
end

function Handler:s2cTurn(msg)
	g.log:info("s2cTurn data:", msg)
end

function Handler:s2cRiver(msg)
	g.log:info("s2cRiver data:", msg)
end

function Handler:s2cGameover(msg)
	g.log:info("s2cGameover data:", msg)
end

function Handler:s2cPot(msg)
	g.log:info("s2cPot data:", msg)
end

function Handler:s2cEnter(msg)
	g.log:info("s2cEnter data:", msg)
end

function Handler:s2cUpblindBc(msg)
	g.log:info("s2cUpblindBc data:", msg)
end

function Handler:eventBc(msg)
	local data = {}
	print("msg:", msg.msg)
	if msg.msg then
		data = json.decode(msg.msg)
	end
	g.log:info("s2cUpblindBc data:", data)
end

function Handler:ntUpdateUser(msg)
	g.log:info("ntUpdateUser data:", msg)
end

function Handler:reqShowCard()

end

function Handler:reqMessage()
	local data = {
		mid = self.uid,
		tid = self.tid,
		msg = 	"{\"type\":\"chat\",\"msg\":\"11111他的妈\"}"
	}
	self.client_net:send("C2_TS_message", data)

end

function Handler:ntMessage(msg)
	g.log:info("ntMessage data:", msg)
end

function Handler:ntError(msg)
	g.log:err("ntError data:", msg)
	self.err_msg = msg
end

function Handler:resMessage(msg)
	g.log:info("resMessage data:", msg)
end

function Handler:ntAutoOper(msg)

end

function Handler:ntShowCards(msg)

end

function Handler:resNextOper(msg)

end

function Handler:s2cHitBall(msg)
	g.log:info("s2cHitBall data:", msg)
end


return Handler